//
//  AlertSignView.m
//  CYWFrame
//
//  Created by Macx on 2017/9/29.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AlertSignView.h"
#import "UIView+LZCView.h"

@interface AlertSignView ()

@property (nonatomic, strong) UIView *alertview;
@property (nonatomic, assign) BOOL judge;
@property (nonatomic, strong) id userInfo;

@end


@implementation AlertSignView

- (id)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        //创建遮罩
        UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:mask];
        mask.backgroundColor = [UIColor blackColor];
        mask.alpha = 0.3f;
        [mask addTarget:self action:@selector(maskClick)];
        
        //创建alert
        self.alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-60, 10)];
        self.alertview.layer.cornerRadius = 5;
        self.alertview.clipsToBounds = YES;
        self.alertview.backgroundColor = [UIColor whiteColor];
        [self addSubview:_alertview];
        [self exChangeOut:self.alertview dur:0.2];
        
        
        
        
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 第二次就不需要重新创建
    if (self.judge == 0) {
        
        CGFloat width = self.alertview.width;
        
        // title
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, width/2, 40)];
        [self.alertview addSubview:title];
        title.text = @"每日签到";
        title.textColor = kGrayColor;
        title.font = kUHSystemFontWithSize(14);
    
        // close
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.alertview addSubview:closeBtn];
        [closeBtn setImage:[UIImage imageNamed:@"live_zhubo_close"] forState:UIControlStateNormal];
        closeBtn.frame = CGRectMake(width-40, 0, 40, 40);
        [closeBtn addTarget:self action:@selector(maskClick) forControlEvents:UIControlEventTouchUpInside];
        
        // 分割线
        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, title.height, width, 1)];
        [self.alertview addSubview:line1];
        line1.backgroundColor = UISeparatedColorRGB;
        
        NSArray *numArr = @[@"sign_first", @"sign_second", @"sign_third", @"sign_fourth", @"sign_fifth", @"sign_sixth", @"sign_seventh"];
        
        CGFloat imgW = (width-50)/4;
        CGFloat imgH = imgW+imgW/4;
        CGFloat imgY = line1.y+1+10;
        int count = 0;
        
        NSString *history = _userInfo[@"table"][@"history"];
        
        for (int i = 0; i < numArr.count; i++) {
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10+(10+imgW)*count, imgY, imgW, imgH)];
            [self.alertview addSubview:img];
            img.image = IMAGE(numArr[i]);
            count++;
            if (i==3) {
                count=0;
                imgY = 10+imgY+imgH;
            }
            
            UILabel *score = [[UILabel alloc] initWithFrame:CGRectMake(0, imgH/3*2, imgW, imgH/3)];
            [img addSubview:score];
            score.text = [NSString stringWithFormat:@"+%@", _userInfo[@"score"]];
            score.textColor = kGrayColor;
            score.font = kUHSystemFontWithSize(10);
            score.textAlignment = NSTextAlignmentCenter;
            
            NSRange range = {i,1}; //1.表示截取的位置下标，6:截取的长度
            NSString *str = [history substringWithRange:range];
            NSLog(@"+++++%@", str);
            
            if ([str isEqualToString:@"1"]) {
                UIImageView *img2 = [[UIImageView alloc] init];
                img2.frame = img.frame;
                [self.alertview addSubview:img2];
                img2.image = IMAGE(@"sign_finish");
                img2.alpha = 0.8f;
            }
            
        }
        
        CGSize size2 = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(10)];
        UILabel *intro = [[UILabel alloc] initWithFrame:CGRectMake(10, imgY+20+imgH, width/2, size2.height)];
        [self.alertview addSubview:intro];
        intro.text = @"签到送积分";
        intro.textColor = kGrayColor;
        intro.font = kUHSystemFontWithSize(10);
        
        UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.alertview addSubview:signBtn];
//        [signBtn setTitle:@"我要签到" forState:UIControlStateNormal];
        signBtn.frame = CGRectMake(10, intro.y+intro.height+10, width-20, 40);
        [signBtn addTarget:self action:@selector(signClick:) forControlEvents:UIControlEventTouchUpInside];
        signBtn.backgroundColor = UIMainColorRGB;
        if ([[NSString stringWithFormat:@"%@", self.userInfo[@"is_signed"]] isEqualToString:@"1"]) {
            [signBtn setTitle:@"已签到" forState:UIControlStateNormal];
        }
        else
            [signBtn setTitle:@"我要签到" forState:UIControlStateNormal];
    
        self.alertview.frame = CGRectMake(0, 0, SCREEN_WIDTH-60, signBtn.y+signBtn.height+20);
        self.alertview.center = self.center;
        
    }
    self.judge = YES;
    
}


// 构建礼物和观看人数视图
- (UIView *)createColumn:(NSString *)column {
    
    UIView *bgView = [[UIView alloc] init];
    CGFloat width = self.alertview.width/4;
    
    CGSize textSize = [LZCTools textSizeWithText:column font:kUHSystemFontWithSize(11)];
    
    UILabel *num = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, textSize.height)];
    [bgView addSubview:num];
    num.textColor = [UIColor grayColor];
    num.font = kUHSystemFontWithSize(11);
    num.text = @"123";
    num.textAlignment = NSTextAlignmentCenter;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, textSize.height+SW(40), width, textSize.height)];
    [bgView addSubview:label];
    label.textColor = [UIColor lightGrayColor];
    label.font = kUHSystemFontWithSize(11);
    label.text = column;
    label.textAlignment = NSTextAlignmentCenter;
    
    return bgView;
}

#pragma mark ==========响应方法==========
// 弹出
-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
    
}
- (void)maskClick
{
    // 取消弹框
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.alertview = nil;
        [self removeFromSuperview];
    }];
}

- (void)signClick:(UIButton *)sender {
    NSLog(@"签到");
    UIViewController *currVC = [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];

    if ([sender.titleLabel.text isEqualToString:@"已签到"]) {
        [self maskClick];
        [currVC presentViewController:[LZCTools alertMessage:@"已经签到"] animated:YES completion:nil];
        return;
    }
    
    [[NetworkTool sharedTool] requestWithName:@"签到" URLString:APISigns parameters:nil method:@"POST" callBack:^(id responseObject) {
//        [sender setTitle:@"已签到" forState:UIControlStateNormal];
        [LZCTools HUDMessage:@"签到成功" delay:1.0f];
        [self maskClick];
    }];
}

- (void)initShow:(id)userInfo {
    NSLog(@"可以弹出alert");
    self.userInfo = userInfo;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
